package com.loanapp.server.market.provider.modular.bbs.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.loanapp.base.common.exception.BusinessException;
import com.loanapp.base.common.utils.PhoneUtils;
import com.loanapp.base.common.utils.StringUtil;
import com.loanapp.base.database.common.base.SqlMapper;
import com.loanapp.server.market.api.bbs.entity.Post;
import com.loanapp.server.market.api.bbs.query.PostQuery;
import com.loanapp.server.market.provider.modular.bbs.mapper.PostMapper;
import com.loanapp.server.market.provider.modular.bbs.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
public class PostServiceImpl implements PostService {


    @Autowired
    private PostMapper mapper;
    @Autowired
    private SqlMapper sqlMapper;


    public PageInfo<Map<String, Object>> getPostListPage(PostQuery query, int[] pageParams) {
        PageHelper.startPage(pageParams[0],pageParams[1]);
        String sql="select id,img_url as imgUrl,title,update_time as time,(select COUNT(*) from bbs_comment where post_id=a.id) as num from bbs_post_base a " +
                "order by top_rank DESC,update_time desc";
        List<Map<String, Object>> list =sqlMapper.selectList(sql);
        PageInfo pageInfo = new PageInfo(list);
        if(pageParams[0]>pageInfo.getPageNum()){
            pageInfo.setList(new ArrayList());
        }
        return  pageInfo;
    }

    @Override
    public Map<String, Object> getPostWithCommentById(Long id) throws  BusinessException {
        String postSql="select id,post_pid pid,post_pname pName,title,content,img_url as imgUrl,create_time as cTime from bbs_post_base where id=#{id}";
        Map<String, Object> postInfo =sqlMapper.selectOne(postSql,id);
        if (postInfo!=null) {
            String pname = (String) postInfo.get("pName");
            if (StringUtil.isNotEmpty(pname)) {
                if (PhoneUtils.isMobile(pname)) {
                    postInfo.put("pName", PhoneUtils.phoneWtihAsterisk(pname));
                }
            }
        }
        String commentSql="select post_pname as name,content from bbs_comment  where post_id=#{id} order by create_time desc  limit 0,5";
        List<Map<String,Object>> commentList=sqlMapper.selectList(commentSql,id);
        for( Map map : commentList){
            String phone= (String) map.get("name");
            if(StringUtil.isNotEmpty(phone)){
               if(PhoneUtils.isMobile(phone)){
                  map.put("name",PhoneUtils.phoneWtihAsterisk(phone));
               }
            }
        }
        Map<String,Object> map=new HashMap<>();
        map.put("postInfo",postInfo);
        map.put("commentList",commentList);
        return map;
    }

    @Transactional
    public Long Insert(Post post) throws BusinessException {
        Date date=new Date();
        post.setCreateTime(date);
        post.setUpdateTime(date);
        Integer rs=mapper.insert(post);
        return rs>0?post.getId():null;
    }

    @Override
    public boolean DeleteByPk(Long aLong) throws BusinessException {
        return false;
    }

    @Override
    public boolean Update(Post post) throws BusinessException {
        return false;
    }

    @Override
    public Post selectByPk(Long aLong) throws BusinessException {
        return null;
    }

    @Override
    public Post selectByModel(PostQuery postQuery) throws BusinessException {
        return null;
    }

    @Override
    public Post selectBySelf(Post post) throws BusinessException {
        return null;
    }

    @Override
    public List<Post> selectListByModel(PostQuery postQuery) throws BusinessException {
        return null;
    }

    @Override
    public Long selectCountByQueryMap(PostQuery postQuery) {
        return null;
    }

    @Override
    public List<Post> selectPageListByQueryMap(PostQuery postQuery) throws BusinessException {
        return null;
    }

    @Override
    public boolean CanPost(Long borrowerId) throws BusinessException {
        String sql="select count(*) from bbs_gap where uid=#{borrowerId}";
        Integer rs= sqlMapper.selectOne(sql,borrowerId,Integer.class);
        return rs>0?false:true;
    }
}
